package cn.icanci.loopstack.plugin.dalgen.factory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cn.hutool.core.collection.CollectionUtil;
import cn.icanci.loopstack.plugin.dalgen.config.GlobalConfig;
import cn.icanci.loopstack.plugin.dalgen.model.ClassInfoVO;
import cn.icanci.loopstack.plugin.dalgen.model.ConfigurationInfoVO;
import cn.icanci.loopstack.plugin.dalgen.util.DataBaseUtil;

/**
 * ClassInfoFactory
 * 
 * @author icanci
 * @since 1.0 Created in 2022/05/13 18:12
 */
public class ClassInfoFactory {

    private volatile static List<ClassInfoVO> CLASS_INFO_LIST = new ArrayList<>();

    /**
     * 获取 ClassInfoVO
     * 
     * @return 返回 ClassInfoVO
     */
    public static List<ClassInfoVO> getClassInfoList() {
        if (CollectionUtil.isEmpty(CLASS_INFO_LIST)) {
            synchronized (ClassInfoFactory.class) {
                if (CollectionUtil.isEmpty(CLASS_INFO_LIST)) {
                    try {
                        // 获取配置项
                        ConfigurationInfoVO config = GlobalConfig.getGobleConfig();

                        List<String> tableNames = DataBaseUtil.getAllTableNames();
                        Set<String> removePreTables = config.getRemovePreTables();
                        for (String tableName : tableNames) {
                            // 仅加载 *; 配置项 或者 include包含项才进行处理
                            if ("*;".equals(config.getInclude()) || config.getIncludeMap().containsKey(tableName)) {
                                ClassInfoVO classInfo = DataBaseUtil.parseClassInfo(tableName, removePreTables);
                                CLASS_INFO_LIST.add(classInfo);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return CLASS_INFO_LIST;
    }
}
